package com.atgugu.flink.chapter07.state;

import com.atgugu.flink.bean.WaterSensor;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lzc
 * @Date 2022/4/6 10:02
 */
public class Flink06_KeyedState_Reduce {
    public static void main(String[] args) throws Exception {
        
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        // 当水位超过20的时候, 5s之后发出预警
        env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
            })
            .keyBy(WaterSensor::getId)
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
    
    
                private ReducingState<Integer> vcSumState;
    
                @Override
                public void open(Configuration parameters) throws Exception {
                    // 参与具体的数据类型, 和聚合后的结果类型要保持一致
                    /*vcSumState = getRuntimeContext().getReducingState(
                        new ReducingStateDescriptor<Integer>(
                            "vcSumState",
                            new ReduceFunction<Integer>() {
                                @Override
                                public Integer reduce(Integer value1,
                                                      Integer value2) throws Exception {
                                    return value1 + value2;
                                }
                            },
                            Integer.class
                        )
                    );*/
    
                    /*vcSumState = getRuntimeContext().getReducingState(
                        new ReducingStateDescriptor<Integer>(
                            "vcSumState",
                           (value1, value2) -> value1 + value2,
                            Integer.class
                        )
                    );*/
    
                    vcSumState = getRuntimeContext().getReducingState(
                        new ReducingStateDescriptor<Integer>(
                            "vcSumState",
                            Integer::sum,
                            Integer.class
                        )
                    );
                }
                
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    vcSumState.add(value.getVc());
                    
                    out.collect(ctx.getCurrentKey() + " 的水位和是: " + vcSumState.get());
                }
                
                
            })
            .print();
    
        env.execute();
    }
}
